package com.tvshows.structure.bdd.repositories;

import java.util.Calendar;
import java.util.Date;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.tvshows.structure.bdd.repositories.interfaces.HistoriqueRepository;
import com.tvshows.structure.beans.Favorite;
import com.tvshows.structure.beans.Historique;

@Repository( "historiqueRepository" )
public class HistoriqueRepositoryImpl
    implements HistoriqueRepository
{
    @Autowired
    private SessionFactory sessionFactory;

    @Override
    @Transactional
    public void addHistorique( Historique historique )
    {
        sessionFactory.getCurrentSession().save( historique );
    }


    @Override
    @Transactional
    public Historique findByShow( Favorite favorite )
    {
        Date today = Calendar.getInstance().getTime();


        return (Historique) sessionFactory.getCurrentSession().getNamedQuery( "historique.byShow" )
            .setParameter( "favorite", favorite ).setParameter( "today", today ).uniqueResult();
    }

}
